import Vuex from 'vuex'

const default_theme = "normal";

export const menus = {};

export const SESSION_TOKEN_NAME = process.env.VUE_APP_TOKEN_NAME;

export const setCookie = (name, value) => {
    window.localStorage.setItem(name, value);
}

export const getCookie = (name) => {
    return window.localStorage.getItem(name);
}

export const saveSessionId = (sesssionId) => {
    setCookie(SESSION_TOKEN_NAME, sesssionId);
}

export const getSessionId = () => {
    return getCookie(SESSION_TOKEN_NAME);
}

function initMenus(items, parent) {
    for(var i=0; i<items.length; i++) {
        initMenu(items[i], parent);
    }
}

function initMenu(item, parent) {
    item.parent = parent;
    menus[item.id] = item;
    if(item.children) {
        initMenus(item.children, item);
    }
}

export let AllowedRouters = [];

export const store = {
    state() {
        return {
            loading: false,
            menus: [],
            theme: null,
            //头像
            avatar: '',
            //账号信息
            account: {},
            fullscreen: false
        }
    },
    mutations: {
        setMenus(state, menus) {
            state.menus = menus;
            initMenus(menus, null);
        },
        setAvatar(state, avatar) {
            state.avatar = avatar;
        },
        loading(state, loading) {
            state.loading = loading;
        },
        activeMenu(state, event) {
            state.menus[event.index].active = event.active;
        },
        changeFullscreen(state) {
            var fullscreen = document.isFullScreen || 
                              document.mozIsFullScreen || 
                              document.webkitIsFullScreen || 
                              document.msIsFullScreen || false;
            if(fullscreen) {
                document.exitFullscreen?document.exitFullscreen():
                document.mozCancelFullScreen?document.mozCancelFullScreen():
                document.webkitExitFullscreen?document.webkitExitFullscreen():'';
            } else {
                var element = document.body;
                var requestMethod =element.requestFullScreen
                     ||element.webkitRequestFullScreen //谷歌
                     ||element.mozRequestFullScreen  //火狐
                     ||element.msRequestFullScreen; //IE11
                if (requestMethod) {     
                    requestMethod.call(element);   
                } else if (typeof window.ActiveXObject !== "undefined") {  
                    var wscript = new ActiveXObject("WScript.Shell"); 
                    if (wscript !== null) {    //创建成功
                         wscript.SendKeys("{F11}");//触发f11   
                    }   
                }
            }
            state.fullscreen = !fullscreen;
        },
        setTheme(state, theme) {
            state.theme = theme;
        },
        init(state, res) {
            if(res.success) {
                AllowedRouters = res.data.routers;
                //设置菜单
                state.menus = res.data.menus;
                initMenus(state.menus, null);
                state.theme = res.data.desktop ? res.data.desktop : default_theme;
                //头像
                if(res.data.avatar) {
                    state.avatar = res.data.avatar;
                }
                //账号信息
                state.account = res.data.account;
            } else {
                AllowedRouters = [];
                state.menus = [];
                state.theme = '';
                state.avatar = '';
                state.account = {};
            }
        }
    }
};
export default Vuex;